//
//  TwoTableViewCell.m
//  fp
//
//  Created by xingshuai on 2020/11/10.
//

#import "TwoTableViewCell.h"

#import "XATextView.h"

@interface TwoTableViewCell () <XATextViewDelegate>

@property (nonatomic, strong) UILabel *topTitle;        // 标题
@property (nonatomic, strong) UILabel *numTextLabel;    // 反馈文字字数
@property (nonatomic, strong) XATextView *feedText;     // 反馈文字
@property (nonatomic, strong) UILabel *placeLabel;      // 提示文字

@end

@implementation TwoTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView  {
    static NSString *identifier = @"XAFeedDesCellID";
    id cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self creatViewUI];
    }
    return self;
}

-(void)creatViewUI {
    self.topTitle = [[UILabel alloc]init];
    self.topTitle.text = @"请选择反馈类型";
    self.topTitle.textColor = [UIColor colorWithHexString:@"#3B3B3B"];
    self.topTitle.font = [UIFont pingFangSCWithWeight:FontWeightStyleSemibold size:20];
    [self.contentView addSubview:self.topTitle];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView).offset(30);
        make.height.mas_equalTo(28);
    }];
    
    self.numTextLabel = [[UILabel alloc]init];
    self.numTextLabel.text = @"0/200";
    self.numTextLabel.textColor = [UIColor colorWithHexString:@"#AFAFAF"];
    self.numTextLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    [self.contentView addSubview:self.numTextLabel];
    [self.numTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topTitle);
        make.left.equalTo(self.topTitle.mas_right).offset(10);
    }];
    
    self.feedText = [[XATextView alloc]initWithFrame:CGRectZero];
    self.feedText.textColor = [UIColor colorWithHexString:@"#3B3B3B"];
    self.feedText.realDelegate = self;
    self.feedText.scrollEnabled = NO;
    self.feedText.maxLength = 200;
    self.feedText.inputLength = 200;
    self.feedText.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    [self.contentView addSubview:self.feedText];
    [self.feedText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.topTitle.mas_bottom).offset(15);
        make.bottom.equalTo(self.contentView).offset(-15);
    }];
    
    self.placeLabel = [[UILabel alloc]init];
    self.placeLabel.enabled = NO;
    self.placeLabel.backgroundColor = [UIColor clearColor];
    self.placeLabel.numberOfLines = 0;
    self.placeLabel.text = FeedDescrition;
    self.placeLabel.font =  [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    self.placeLabel.textColor = [UIColor colorWithHexString:@"#888888"];
    [self.feedText addSubview:self.placeLabel];
    [self.placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.feedText).offset(6);
        make.top.equalTo(self.feedText).offset(8);
    }];
    
}

-(void)setFeedString:(NSString *)feedString {
    _feedString = feedString;
    if (feedString.length > 0) {
        self.placeLabel.hidden = YES;
        self.feedText.text = self.feedString;
    } else {
        self.placeLabel.hidden = NO;
        self.feedText.text = @"";
    }
}

-(void)XATextViewDidChange:(UITextView *)textView{
    // 反馈内容大于0时隐藏提示文字，否则显示
    if (textView.text.length > 0) {
        self.placeLabel.hidden = YES;
    } else {
        self.placeLabel.hidden = NO;
    }
    
    // 更新当前字数
    self.numTextLabel.text = [NSString stringWithFormat:@"%ld/200",(long)textView.text.length];
    
    if ([_delegate respondsToSelector:@selector(editFeedContentTextString:)]) {
        [_delegate editFeedContentTextString:textView.text.length > 0 ? textView.text : FeedDescrition];
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
